﻿using Sunny.UI;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIKeyboardItem : UIDropDownItem
    {
        public UIKeyboardItem()
        {
            InitializeComponent();
        }

        private void btnCaps_Click(object sender, System.EventArgs e)
        {
            btnCaps.Selected = !btnCaps.Selected;
            foreach (var item in this.GetControls<UIButton>())
            {
                if (item.Name.Length == 1)
                    item.Text = btnCaps.Selected ? item.Text.ToUpper() : item.Text.ToLower();
            }

            SetState(VirtualKeys.VK_CAPITAL, btnCaps.Selected);
            //Close();
        }

        [DllImport("user32.dll")]
        static extern short GetKeyState(int nVirtKey);
        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        public static bool GetState(VirtualKeys Key)
        {
            return (GetKeyState((int)Key) == 1);
        }

        const uint KEYEVENTF_EXTENDEDKEY = 0x1;
        const uint KEYEVENTF_KEYUP = 0x2;

        public static void SetState(VirtualKeys Key, bool State)
        {
            if (State != GetState(Key))
            {
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
                keybd_event((byte)Key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
        }

        public enum VirtualKeys : byte
        {
            VK_NUMLOCK = 0x90, //数字锁定键
            VK_SCROLL = 0x91,  //滚动锁定
            VK_CAPITAL = 0x14, //大小写锁定
            VK_A = 62
        }

        private void UIKeyboard_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                Rectangle rect = item.Bounds;
                rect.Offset(1, 1);
                e.Graphics.DrawRoundRectangle(Color.Gray, rect, 9);
            }
        }

        private void btnOK_Click(object sender, System.EventArgs e)
        {
            UIButton btn = (UIButton)sender;
            DoValueChanged(this, btn.Tag.ToString().ToInt());
            Close();
        }

        private void btnBackspace_Click(object sender, System.EventArgs e)
        {
            UIButton btn = (UIButton)sender;
            int tag = btn.Tag.ToString().ToInt();
            if (btn.Name.Length == 1)
            {
                DoValueChanged(this, tag - 32);
            }
            else
            {
                DoValueChanged(this, tag);
            }
        }

        public void SetCaps(bool caps)
        {
            btnCaps.Selected = caps;
            foreach (var item in this.GetControls<UIButton>())
            {
                if (item.Name.Length == 1)
                    item.Text = btnCaps.Selected ? item.Text.ToUpper() : item.Text.ToLower();
            }
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            bool CapsState = GetState(VirtualKeys.VK_CAPITAL);
            if (CapsState != btnCaps.Selected)
            {
                SetCaps(CapsState);
            }
        }

        private void UIKeyboardItem_VisibleChanged(object sender, System.EventArgs e)
        {
            timer1.Enabled = Visible;
        }

        public override void SetStyleColor(UIBaseStyle uiColor)
        {
            base.SetStyleColor(uiColor);
            BackColor = FillColor = RectColor = Color.FromArgb(238, 238, 238);
        }
    }
}
